﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using LevelEditor.App.Serialization;
using LevelEditor.Objects.Waypoints;
using LevelEditor.Objects.Tiles;

namespace LevelEditor.Util
{
    class Util
    {
        public static float Distance(Point a, Point b)
        {
            int x = b.X - a.X;
            int y = b.Y - a.Y;

            return (float)Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, y));
        }

        /// <summary>
        /// Gets the column and the row in the format  --->  (row, col)
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="clickedPoint">MUST BE USED PointToClient</param>
        /// <returns></returns>
        public static Point GetPointInTablePanel(TableLayoutPanel panel, Point clickedPoint)
        {
            if (clickedPoint.X > panel.Width || clickedPoint.Y > panel.Height)
                return Point.Empty;

            int w = panel.Width;
            int h = panel.Height;
            int[] widths = panel.GetColumnWidths();

            int i;
            for (i = widths.Length - 1; i >= 0 && clickedPoint.X < w; i--)
                w -= widths[i];
            int col = i + 1;

            int[] heights = panel.GetRowHeights();
            for (i = heights.Length - 1; i >= 0 && clickedPoint.Y < h; i--)
                h -= heights[i];

            int row = i + 1;

            return new Point(col, row);

        }

        public static byte[] ConvertImageToByte(Image img)
        {
            if (img == null) return new byte[0];

            byte[] imageBuffer = null;
            MemoryStream mStream = new MemoryStream();
            img.Save(mStream, System.Drawing.Imaging.ImageFormat.Png);
            BitmapDecoder decoder = BitmapDecoder.Create(mStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);

            BitmapSource bitmapSourcce = decoder.Frames[0];

            imageBuffer = null;

            if (img != null)
            {
                using (var stream = new MemoryStream())
                {
                    var encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bitmapSourcce));
                    encoder.Save(stream);
                    imageBuffer = stream.ToArray();
                }
            }
            return imageBuffer;
        }


        public static Size SizeToNewScreen(Size oldSize, Size screenSize, Size destScreenSize)
        {
            float l = oldSize.Width / (float)screenSize.Width;
            float nl = oldSize.Width / (float)destScreenSize.Width;

            float newWidth = (oldSize.Width / nl);
            newWidth = (newWidth * l);

            if (oldSize.Width == oldSize.Height) return new Size((int)newWidth, (int)newWidth);

            l = oldSize.Height / (float)screenSize.Height;
            nl = oldSize.Height / (float)destScreenSize.Height;

            float newHeight = (oldSize.Height / nl);
            newHeight = (newHeight * l);

            return new Size((int)newWidth, (int)newHeight);
        }

        public static Point LocationToNewScreen(Point oldLocation, Size screenSize, Size destScreenSize)
        {
            float l = oldLocation.X / (float)screenSize.Width;
            float nl = oldLocation.X / (float)destScreenSize.Width;

            float newX = (oldLocation.X / nl);
            newX = (newX * l);

            l = oldLocation.Y / (float)screenSize.Height;
            nl = oldLocation.Y / (float)destScreenSize.Height;

            float newY = (oldLocation.Y / nl);
            newY = (newY * l);

            return new Point((int)newX, (int)newY);
        }

        public static Image Base64ToImage(string base64String)
        {
            byte[] b = Convert.FromBase64String(base64String);
            MemoryStream stream = new MemoryStream(b);

            return Bitmap.FromStream(stream);
        }



        public static int GetAmountOfWaypointsCollections()
        {
            List<int> IdsCollection = new List<int>();

            foreach(WaypointsCollection collection in UtilSerialization.allWaypointsCollections)
            {
                foreach (Waypoint waypoint in collection)
                {
                    if (!IdsCollection.Contains(waypoint.idCollection))
                    {
                        IdsCollection.Add(waypoint.idCollection);
                    }
                }
            }


            return IdsCollection.Count;
        }
    }
}
